//
// Created by Key-Box-Min on 31/05/2020.
//

#ifndef UNTITLED_OBJECT_H
#define UNTITLED_OBJECT_H

#include <set>
#include <memory>
#include <atomic>
#include <iostream>
#include <mutex>

#include "EventSystem/SignalEventDrivers.h"
#include "AlgorihmTemplate.h"
#include "../Tools.h"

#define LIB_OBJECT LIB_TYPE_SUB_CLASS(Type,Object)
#define LIB_SIGNAL_OBJ(T,...) Signal<__VA_ARGS__> T = Signal<__VA_ARGS__>(loop());

namespace Lib{

    class Object : public EventDriver {
    public:
        using Type = uint32_t;
        LIB_TYPE_BASE_CLASS(Type)

    public:
        using Option = SignalBase::Option;
        LIB_SIGNAL_OBJ(loop_start)
        LIB_SIGNAL_OBJ(loop_stop)

    public:
        Object(Object* parent = nullptr);
        virtual ~Object();

        Object* parent() const {return __parent;}
        void setParent(Object* parent);
        bool hasSon(Object* son);

        EventLoop*& loop();
        void setLoop(EventLoop* loop);

    public:
        friend bool operator <(const Object& obj_a,const Object& obj_b)
        {return obj_a.getType() < obj_b.getType();}

    protected:
        bool filter(EventAgent& event) override ;
        void event(EventAgent& event) override ;
        void delete_begin();

    private:
        std::atomic<Object*>    __parent;
        TSet<Object*>           __sons;
    };



}

#endif //UNTITLED_OBJECT_H
